package com.robot.ad.controller;

import com.alibaba.fastjson.JSON;
import com.robot.ad.pojo.Ad;
import com.robot.ad.service.AdService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ad控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/ad")
public class AdJsonController {
    @Autowired
    private AdService adService;

    /**
     * ad表type 广告类型 0-图片 1-视频
     */
    private String adTypeImage = "0";
    private String adTypeVideo = "1";
    /**
     * ad表position 广告位置 0-首页广告 1-回调广告
     */
    private String adPositionHome = "0";
    private String adPositionCallback = "1";
    /**
     * ad表status 状态0-禁用 1-启用
     */
    private String adStatusDisable = "0";
    private String adStatusEnable = "1";


    /**
     * 广告列表-条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "third/findAdBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public String thirdFindAdBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Ad> pageList = adService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据ID查询广告详情
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "third/findAdById/{id}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String thirdFindAdById(@PathVariable String id) {
        return JSON.toJSONString(adService.findById(id));
    }

    /**
     * 增加广告
     *
     * @param ad
     */
    @RequestMapping(value = "third/addAdByAd", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public void thirdAddAdByAd(@RequestBody String ad) {
        adService.add(JSON.parseObject(ad, Ad.class));
    }

    /**
     * 修改广告
     *
     * @param ad
     */
    @RequestMapping(value = "third/updateAdByAd", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    public void thirdUpdateAdByAd(@RequestBody String ad) {
        adService.update(JSON.parseObject(ad, Ad.class));
    }

    /**
     * 删除广告
     *
     * @param id
     */
    @RequestMapping(value = "third/deleteAdById/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8")
    public void thirdDeleteAdById(@PathVariable String id) {
        adService.deleteById(id);
    }

    /**
     * 根据position和status查找广告列表
     *
     * @param status
     * @return
     */
    @GetMapping(value = "third/findAdsByStatusToIndex/{status}", produces = "application/json;charset=utf-8")
    public String thirdFindAdsToIndex(@PathVariable String status) {
        return JSON.toJSONString(adService.findTop2ByPositionAndStatusAndDeletedAtIsNullOrderBySortDesc("0", status));
    }

    /**
     * 根据position和status查找广告列表
     *
     * @param position
     * @param status
     * @return
     */
    @GetMapping(value = "third/findAdsByPositionAndStatus/{position}/{status}", produces = "application/json;charset=utf-8")
    public String thirdFindTop2ByPositionAndStatusOrderByCreatedAt(@PathVariable String position, @PathVariable String status) {
        return JSON.toJSONString(adService.findTop2ByPositionAndStatusAndDeletedAtIsNullOrderBySortDesc(position, status));
    }
}
